//
//  NetworkManager.m
//  SchoolHelper
//
//  Created by 任辰东 on 2018/3/26.
//  Copyright © 2018年 JSY. All rights reserved.
//

#import "NetworkManager.h"

@implementation NetworkManager

+ (instancetype)sharedManager
{
    static NetworkManager *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedClient = [[NetworkManager alloc] init];
        _sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
        _sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
//        _sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
        [_sharedClient.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    
    });
    
    return _sharedClient;
}

- (NSString *)requestApiWithPath:(NSString *)path
{
    path = [NSString stringWithFormat:@"%@%@",kBaseApi,path];
    return path;
}

-(void)getDataWithURL:(NSString *)urlString withParams:(NSDictionary *)params onCompletion:(requestSuccessBlock)completionBlock onError:(requestFailureBlock)errorBlock
{
    NSString *url = [self requestApiWithPath:urlString];
    
    [self GET:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        id responseData = [responseObject objectForKey:@"data"];
        
        NSNumber *code = (NSNumber *)[responseObject objectForKey:@"code"];
        NSString *msg = (NSString *)[responseObject objectForKey:@"msg"];
        
        
        if ([code  isEqual:@(1)]) {
            
            completionBlock(code,msg,responseData);
            
        }else{
            errorBlock(code,msg);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
    
}


-(void)postDataWithURL:(NSString *)urlString withParams:(NSDictionary *)params onCompletion:(requestSuccessBlock)completionBlock onError:(requestFailureBlock)errorBlock
{
    NSString *url = [self requestApiWithPath:urlString];
    
    DLog(@"url === %@\nparams === %@",url,params);
    
    [self POST:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DLog(@"responseObject == %@",responseObject);
        id responseData = [responseObject objectForKey:@"data"];
        
        NSNumber *code = (NSNumber *)[responseObject objectForKey:@"code"];
        NSString *msg = (NSString *)[responseObject objectForKey:@"msg"];
        
        
        if ([code  isEqual:@(1)]) {
            
            completionBlock(code,msg,responseData);
            
        }else{
            errorBlock(code,msg);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(@(1000),@"服务器维护中...");
        DLog(@"%@",error);
        
    }];
    
}

@end
